<?php
/**
 * Created by PhpStorm.
 * User: 沁塵
 * Date: 2019/4/20
 * Time: 19:57
 */

namespace app\api\controller\v1;

use app\api\model\Material as MaterialModel;
use app\api\model\ModelEvent;
use app\api\model\ProductScript;
use think\Request;

class Material
{
    public function checkMaterial(Request $request) {
        $params = $request->post();
        $event = new MaterialModel();
        $count = $event->where("date", $params["date"])
            ->where("pid", $params["pid"])
            ->where("filename", $params["filename"])
            ->count();
         if ($count == 0) {
             return writeJson(200, false, '请求成功');
         } else {
             return writeJson(200, true, '请求成功');
         }

    }

    public function checkMaterial1(Request $request) {
        $params = $request->post();
        $event = new MaterialModel();
        $count = $event->where("model", $params["model"])
            ->where("pid", $params["pid"])
            ->where("filename", $params["filename"])
            ->where("type", 1)
            ->count();
        if ($count == 0) {
            return writeJson(200, false, '请求成功');
        } else {
            return writeJson(200, true, '请求成功');
        }

    }

    public function addMaterial(Request $request) {
        $params = $request->post();
        $event = new MaterialModel();
        $event->create($params);

        return writeJson(200, '', '请求成功');
    }

    public function getMaterial(Request $request)
    {
        $params = $request->post();
        $event = new MaterialModel();
        $list = $event->where("num", "<", 2)
            ->where("pid", $params["pid"])
            ->orderRand()->limit(3)
            ->select()->toArray();

        $savedata = [];
        if (!empty($list) && count($list) == 3) {
            foreach ($list as $li) {
                $old_num = $li["num"];
                $item = [];
                $item["id"] = $li["id"];
                $item["num"] = $old_num + 1;
                $savedata[] = $item;
            }
            $event->saveAll($savedata);
            return writeJson(200,$list,'成功！');
        } else {
            return writeJson(200,[],'成功！');
        }


    }

    public function getMaterial_hj(Request $request)
    {
        $params = $request->post();
        $event = new MaterialModel();
        $fifteenDaysAgo = strtotime('-15 days', time());

        $list = $event->where("num", "<", 1)
            ->where("pid", $params["pid"])
            ->where("type", 2)
//            ->where("identifier", $params["identifier"])
            ->where("create_time", ">=", date('Y-m-d H:i:s', strtotime('midnight', $fifteenDaysAgo)))
            ->orderRand()->limit($params["limit"])
            ->select()->toArray();

        $savedata = [];
        $dis = "";
        if (!empty($list) && count($list) == $params["limit"]) {
            foreach ($list as $li) {
                $old_num = $li["num"];
                $item = [];
                $item["id"] = $li["id"];
                $item["num"] = $old_num + 1;
                $savedata[] = $item;
                if ($dis == "") {
                    $dis = strval($li["id"]);
                } else {
                    $dis = $dis . "," . strval($li["id"]);
                }
            }

            $event->saveAll($savedata);
            ModelEvent::where("id", $params["id"])->update(["ids"=>$dis]);

            return writeJson(200,$list,'成功！');
        } else {
            return writeJson(200,[],'成功！');
        }


    }

    public function backfillMaterial(Request $request)
    {
        $params = $request->post();
        $event = new MaterialModel();
        $list = $event->whereIn("id", $params["ids"])->select();
        $savedata = [];
        if (!empty($list)) {
            foreach ($list as $li) {
                $old_num = $li["num"];
                $item = [];
                $item["id"] = $li["id"];
                $item["num"] = $old_num - 1;
                $savedata[] = $item;
            }
            $event->saveAll($savedata);
        }
        return writeJson(200,true,'成功！');

    }

    public function check(Request $request) {
        $params = $request->post();
        $event = new MaterialModel();
        $count = $event->where("pid", $params["pid"])
            ->where("filename", $params["filename"])
            ->count();
        if ($count == 0) {
            return writeJson(200, false, '请求成功');
        } else {
            return writeJson(200, true, '请求成功');
        }

    }

    /**
     * 跑素材是否人设镜头
     */
    public function checkKou()
    {
        $event = new MaterialModel();
        $info = $event->whereNull("is_kou")->where("type", 1)->findOrEmpty();
        if (strpos($info["filename"], "脚本1") !== false) {
            $script_name = 1;
        } else {
            $script_name = 2;
        }
        $product_script = ProductScript::where("pid", $info["pid"])
            ->where("model", $info["model"])
            ->where("script_name", $script_name)->findOrEmpty();
        if (!$product_script->isEmpty()) {
            $script = json_decode($product_script["script"], true);

            foreach ($script as $s) {
                if ($s["v"] == $info["num"]) {
                    $is_kou = $s["kou"];
                    break;
                }
            }
            $info->is_kou = $is_kou;
            $info->save();

            $res = ["id"=>$info["id"],"is_kou"=>$is_kou];
            return writeJson(200, $res, '请求成功');
        } else {
            return writeJson(200, $info["id"], '脚本空');
        }
    }
}